package control;

import dao.GetMeetRoomInfo;
import dao.GetMeetingInfo;
import entity.MeetRoom;
import entity.Meeting;
import entity.User;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.Image;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.util.Callback;
import util.WindowMove;

import java.io.IOException;
import java.net.URL;
import java.time.LocalDateTime;
import java.util.ResourceBundle;

public class HomePageController implements Initializable {
    private User user=null;
    @FXML
    private Label nameLabel,identityLabel,telephoneLabel;

    @FXML
    private TableView<Meeting> manageTable;
    private final ObservableList<Meeting> manageTableSource= FXCollections.observableArrayList();
    @FXML
    private TableView<Meeting> participateTable;
    private final ObservableList<Meeting> participateTableSource=FXCollections.observableArrayList();
    @FXML
    private TableView<MeetRoom> roomTable;
    private final ObservableList<MeetRoom> roomTableSource=FXCollections.observableArrayList();
    @Override
    public void initialize(URL location, ResourceBundle resources){
        nameLabel.setText(user.getName());
        identityLabel.setText(user.getIdentity());
        telephoneLabel.setText(user.getTelephone());
        meetingTableInit(manageTable,manageTableSource);
        meetingTableInit(participateTable,participateTableSource);
        roomTableInit(roomTable,roomTableSource);
        participateTable.setOnMouseClicked(event -> {
            if(event.getClickCount()>=2){
                Meeting meeting=participateTable.getSelectionModel().getSelectedItem();
                FXMLLoader loader=new FXMLLoader(getClass().getResource("/ui/meetingManage.fxml"));
                MeetingManageController meetingManageController=new MeetingManageController(1,meeting);
                loader.setController(meetingManageController);
                Pane root= null;
                try {
                    root = loader.load();
                } catch (Exception e) {
                    System.out.println("我的会议fxml加载失败");
                    e.printStackTrace();
                }
                Scene scene=new Scene(root);
                Stage stage = new Stage();
                stage.setScene(scene);
                stage.initStyle(StageStyle.UNDECORATED); // 设置为无边框
                WindowMove.setMoveMode(stage,root);
                stage.setTitle(meeting.getName());
                stage.getIcons().add(new Image("res/icon_about_selected@3x.png"));
                stage.show();
            }
        });
        manageTable.setOnMouseClicked(event -> {
            if(event.getClickCount()>=2){
                Meeting meeting=manageTable.getSelectionModel().getSelectedItem();
                FXMLLoader loader=new FXMLLoader(getClass().getResource("/ui/meetingManage.fxml"));
                MeetingManageController meetingManageController=new MeetingManageController(2,meeting);
                loader.setController(meetingManageController);
                Pane root= null;
                try {
                    root = loader.load();
                } catch (Exception e) {
                    System.out.println("我的会议fxml加载失败");
                    e.printStackTrace();
                }
                Scene scene=new Scene(root);
                Stage stage = new Stage();
                stage.setScene(scene);
                stage.initStyle(StageStyle.UNDECORATED); // 设置为无边框
                WindowMove.setMoveMode(stage,root);
                stage.setTitle(meeting.getName());
                stage.getIcons().add(new Image("res/icon_about_selected@3x.png"));
                stage.show();
            }
        });
    }
    public HomePageController(User user){
        this.user=user;
        manageTableSource.addAll(GetMeetingInfo.getManage(user.getId()));
        participateTableSource.addAll(GetMeetingInfo.getParticipate(user.getId()));
        roomTableSource.addAll(GetMeetRoomInfo.getMeetingRoom(user.getId()));
    }
    @FXML
    private Button closeButton,minimizeButton;
    @FXML
    private void close(){
        Stage stage = (Stage) closeButton.getScene().getWindow();
        stage.close();
    }
    @FXML
    private void minimize() {
        Stage stage = (Stage) minimizeButton.getScene().getWindow();
        stage.setIconified(true);
    }
    private void meetingTableInit(TableView<Meeting> tableView,ObservableList<Meeting> source){
        ObservableList<TableColumn<Meeting, ?>> manageTableColumnscolumns=tableView.getColumns();
        manageTableColumnscolumns.get(0).setCellValueFactory(new PropertyValueFactory<>("id"));
        manageTableColumnscolumns.get(1).setCellValueFactory(new PropertyValueFactory<>("name"));
        manageTableColumnscolumns.get(2).setCellValueFactory(new PropertyValueFactory<>("room_id"));
        manageTableColumnscolumns.get(3).setCellValueFactory(new PropertyValueFactory<>("begin"));
        manageTableColumnscolumns.get(4).setCellValueFactory(new PropertyValueFactory<>("end"));
        manageTableColumnscolumns.get(5).setCellValueFactory(new PropertyValueFactory<>("status"));
        TableColumn<Meeting,String> column=(TableColumn<Meeting,String>)manageTableColumnscolumns.get(5);
        column.setCellFactory(new Callback<TableColumn<Meeting,String>, TableCell<Meeting,String>>() {
            @Override
            public TableCell<Meeting,String> call(TableColumn<Meeting,String> param) {
                return new TableCell<Meeting,String>() {
                    @Override
                    protected void updateItem(String item, boolean empty) {
                        super.updateItem(item, empty);
                        if (item != null && !empty) {
                            Meeting meeting= getTableView().getItems().get(getIndex());
                            LocalDateTime begin=meeting.getBegin();
                            LocalDateTime end=meeting.getEnd();
                            if (end.compareTo(LocalDateTime.now())<0) {
                                setText("已结束");
                                setStyle("-fx-text-fill: red;");
                            } else if(begin.compareTo(LocalDateTime.now())>0) {
                                setText("未开始");
                                setStyle("-fx-text-fill: #01cfff;");
                            }else{
                                setText("进行中");
                                setStyle("-fx-text-fill: green;");
                            }
                        } else {
                            setText(null);
                        }
                    }
                };
            }
        });
        tableView.setItems(source);
    }
    private void roomTableInit(TableView<MeetRoom> tableView,ObservableList<MeetRoom> source){
        ObservableList<TableColumn<MeetRoom, ?>> roomTableColumnscolumns=tableView.getColumns();
        roomTableColumnscolumns.get(0).setCellValueFactory(new PropertyValueFactory<>("id"));
        roomTableColumnscolumns.get(1).setCellValueFactory(new PropertyValueFactory<>("name"));
        roomTableColumnscolumns.get(2).setCellValueFactory(new PropertyValueFactory<>("position"));
        roomTableColumnscolumns.get(3).setCellValueFactory(new PropertyValueFactory<>("status"));
        tableView.setItems(source);
    }
}
